﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using androidpost.Object;
using Newtonsoft.Json.Linq;
using System.Web.Script.Serialization;
using System.ComponentModel;

namespace androidpost
{
    class Utils1
    {
        public static string getDirName()
        {
           string a=null;
             FolderBrowserDialog FolderBrowserDialog1 = new FolderBrowserDialog();

            FolderBrowserDialog1.ShowNewFolderButton = false;           // Hide 'Make New Folder' button

            FolderBrowserDialog1.Description = "Select Folder to Create Playlist for...";

            // Show the FolderBrowserDialog.
            if (FolderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                a= FolderBrowserDialog1.SelectedPath;
            }

            FolderBrowserDialog1.Dispose();
            return a;
        }

        public static string readFomat() 
        {
            return "*.*";
         
            
        }

        public static void showMB(string value)
        {
            MessageBox.Show(value);
        }

       
        public static void log(string msg)
        {
            System.IO.StreamWriter sw=null ;
            try
            {
                sw = System.IO.File.AppendText("log.txt");
                sw.WriteLine("-----------------------------------------------------------");
                string logLine = System.String.Format(
                    "{0:G}: {1}.", System.DateTime.Now, msg);
                sw.WriteLine(logLine);
                sw.Close();
            }
            catch
            {
 
            }
           
        }
        public static void Clearlog()
        {
            System.IO.File.WriteAllText("log.txt","");
           
        }


        //-----------------------stringToOJStatus-------------------------
        public static OJStatus stringToOJStatus(string text) 
        {
            var JSONObj = new OJStatus();
            JavaScriptSerializer ser = new JavaScriptSerializer();
            try
            {
                JSONObj = ser.Deserialize<OJStatus>(text);

                if (JSONObj.code != 0)
                {
                   
                    Utils1.log("stringToOJStatus() Have error! \n unknow result format");
                }


            }
            catch (Exception e)
            {

                
                Utils1.log("(54457) have error" + e);
            }

            return JSONObj;
        }

        //-----------------------stringToOJControl---------------------------
        public static OJControl stringToOJControl(string text)
        {
            var JSONObj = new OJControl();
            JavaScriptSerializer ser = new JavaScriptSerializer();
            try
            {
                JSONObj = ser.Deserialize<OJControl>(text);

               

            }
            catch (Exception e)
            {

               
                Utils1.log("(7358) have error" + e);
            }

            return JSONObj;
        }

        public static void SynchronizedInvoke(ISynchronizeInvoke sync, Action action)
        {
            // If the invoke is not required, then invoke here and get out.
            if (!sync.InvokeRequired)
            {
                // Execute action.
                action();

                // Get out.
                return;
            }

            // Marshal to the required context.
            try
            {
                sync.Invoke(action, new object[] { });
            }
            catch (Exception)
            {
                
               
            }
           
        }

        public static byte[] encryptData(string data)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] hashedBytes;
            System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
            hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(data));
            return hashedBytes;
        }
        public static string md5(string data)
        {
            return BitConverter.ToString(encryptData(data)).Replace("-", "").ToLower();
        }

        public static Boolean isMediaFile(string fileName)
        {
            for (int i = 0; i <Utils.Constant.listMediaType.Length; i++)
            {
                 if (fileName.IndexOf(Utils.Constant.listMediaType[i])!=-1)return true;
           
            }
            return false;
        }
    }
}
